/** 创建组件对象 * */
var dataPlantForm = new Form();
var dataPlantTable = new Table();
var dataPlantPage = new Page();
var dataPlantDialog = new Dialog();
var dataPlantAjax = new Ajax();
var flag = false;

/** 页面加载完执行 * */
$(function() {
	flag = sessionStorage.orgCode == "*";
	initScript();
	initConditions();
	initDate();
	initButtons();
	// initPage();
	initTable();
	resetTableHeight();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/data/locale/IndicatorDataPlant_" + appLocale + ".js"); // 1.加载国际化js*
	loadScript(appPath + "/project-yddemo/data/properties/IndicatorDataPlantYearProperties.js"); // 2.加载配置js(非必配,配置也可写在本js中)
}

/** 初始化查询条件 * */
function initConditions() {
	dataPlantForm.initTableConditions({
		divId : "dataPlantYearQueryDiv", // 1.归属div*
		type : "table",
		conditions : indicatorDataPlantProperties.getViewConditions(), // 2.配置条件*
		button : { // 4.操作按钮*
			action : "yddemo:plantYear:view",
			width : "120", // 占用宽度*
			align : "center", // left,center,right,百分比*
			label : indicatorDataPlantLocale.text.search, // 文本*
			clickFun : function() { // 按钮事件*
				dataPlantPage.setCurrPage(1);
				pageQuery();
			}
		}
	});

}

/** 初始化按钮 * */
function initButtons() {
	dataPlantTable.initButtons({
		divId : "dataPlantYearButtonDiv", // 1.归属div*
		buttons : indicatorDataPlantProperties.getViewButtons()
	// 2.按钮组配置*
	});
	// if (flag) {
	// setElementsEnabled("yddemo:plantYear:add", "button", false);
	// }
	// disableButton();
}

/** 初始化分页 * */
function initPage() {
	dataPlantPage.initPage({
		divId : "dataPlantYearPageDiv", // 1.归属div*
		eachPageNum : 10, // 2.每页条数(5,10,15,20)*
		pageChangeFun : function() { // 3.分页刷新事件*
			pageQuery();
		}
	});
}

/** 分页查询 * */
function initTable() {
	dataPlantTable.initTable({
		divId : "dataPlantYearTableDiv", // 1.归属div*
		dataAjax : { // 2.发送ajax请求*
			url : requestPath + "/yddemo/plantYears", // 请求url*
			type : "GET"// , // 请求类型*
		// page : dataPlantPage
		// 分页对象(需要分页时配置)
		},
		columns : indicatorDataPlantProperties.getViewColumns(),// 3.表格列配置
		rowClickFun : function(datas) {
		}
	});
	pageQuery();
}

/** 分页查询 * */
function pageQuery() {
	var queryConditions = dataPlantForm.getQueryConditions();
	
	//判断是否能够查询出来数据，如果没有数据提示“无下发指标”;
	var dataObj = dataPlantAjax.sendAjax({ // 获取用户对象
		url : requestPath + "/yddemo/plantYears", // 请求url*
		async : false, // 是否异步*
		type : "GET", // 请求类型*
		data : queryConditions
	});

	if(dataObj != null && dataObj.length > 0){
		dataPlantTable.reloadData(queryConditions);
	}else{
		window.dataPlantDialog.tipDialog(indicatorDataPlantLocale.text.tipNoIndicator, function() {
		});
	}
	
}

function initDate() {
	$("#dataPlantYearQueryDiv").find("[key = 'date']").jeDate({
		isinitVal : true,
		initAddVal : {
			YYYY : 0,
			MM : -0,
			DD : -0
		},
		// //初始化年月日(加减)，common.js提供getJeDateAddVal("2008-08-08")
		// hmsSetVal:{YYYY:2008,MM:8,DD:8,hh:8,mm:8,ss:8}, //1.初始化年月日时分秒
		// 2.初始化时分秒
		ishmsVal : false,
		// minDate: '2016-06-16 23:59:59',
		// maxDate: $.nowDate({DD:0}),
		format : "YYYY",
		zIndex : 3000,
		language : getJeDateLanguage()
	});
}

// 保存
function saveIndicatorDataPlant() {
	var queryConditions = dataPlantForm.getQueryConditions();
	var date = queryConditions.date;

	var datas = dataPlantTable.dataList;
	var values = [];
	$("#dataPlantYearTableDiv input[type='text']").each(function() {
		// alert($(this).val());
		values.push($(this).val());
	});
	if (datas) {
		for (var i = 0; i < datas.length; i++) {
			datas[i].value = values[i];
		}
	}

	dataPlantDialog.confirmDialog(indicatorDataPlantLocale.text.isSave, function() {
		var resultJson = dataPlantAjax.sendAjax({
			url : requestPath + "/yddemo/plantYears", // 1.请求url*
			async : false, // 3.是否异步*
			type : "PUT", // 4.请求类型*
			data : datas,
			params : {
				save : "save",
				date : date
			}
		});
		if (resultJson.success) {
			dataPlantDialog.tipDialog(indicatorDataPlantLocale.text.saveSuccess, function() {
				pageQuery(); // 删除成功后,刷新页面
			});
		} else {
			dataPlantDialog.alertDialog(resultJson.message); // 删除失败后,弹出警告框
		}
	});

}

// 上报
function reportIndicatorDataPlant() {
	var queryConditions = dataPlantForm.getQueryConditions();
	var date = queryConditions.date;

	var datas = dataPlantTable.dataList;
	var values = [];
	$("#dataPlantYearTableDiv input[type='text']").each(function() {
		values.push($(this).val());
	});
	if (datas) {
		for (var i = 0; i < datas.length; i++) {
			datas[i].value = values[i];
		}
	}

	dataPlantDialog.confirmDialog(indicatorDataPlantLocale.text.isReport, function() {
		var resultJson = dataPlantAjax.sendAjax({
			url : requestPath + "/yddemo/plantYears", // 1.请求url*
			async : false, // 3.是否异步*
			type : "PUT", // 4.请求类型*
			data : datas,
			params : {
				report : "report",
				date : date
			}
		});
		if (resultJson.success) {
			dataPlantDialog.tipDialog(indicatorDataPlantLocale.text.reportSuccess, function() {
				pageQuery(); // 删除成功后,刷新页面
			});
		} else {
			dataPlantDialog.alertDialog(resultJson.message); // 删除失败后,弹出警告框
		}
	});
}

// 审批
function approveIndicatorDataPlant() {
}

/**
 * 重设表格高度
 * 
 * @param
 * @return
 */
function resetTableHeight() {
	var leftHeight = $(".dataListDiv").height() - getTotalHeight([ "dataPlantYearQueryDiv", "dataPlantYearButtonDiv" ]);// ,"dataPlantYearPageDiv"
	$("#dataPlantYearTableDiv").height(leftHeight);
}

/**
 * 窗口大小改变时触发
 * 
 * @param
 * @return
 */
$(window).resize(function() {
	resetTableHeight();
});
